<?php

namespace Biz\model\trade;

class OrderExcelLog extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'order_excel_log';


    const ORDER_SHIP_ERROR = 0;//跳过未处理
    const ORDER_SHIP_SUCCESS = 1;//发货成功

    public static $resultDesc = [
        self::ORDER_SHIP_ERROR   => '跳过未处理',
        self::ORDER_SHIP_SUCCESS => '发货成功'
    ];

    public static function create($shopId, $excelId, $orderNo, $acceptName, $excelData, $result = 0)
    {
        $data = [
            'excel_id'    => $excelId,
            'shop_id'     => $shopId,
            'order_no'    => $orderNo,
            'accept_name' => $acceptName,
            'result'      => $result,
            'addtime'     => date('Y-m-d H:i:s')
        ];
        $data = array_merge($data, $excelData);

        $result = self::insert($data);
        if ($result) {
            return self::id();
        }
        return false;
    }

    public static function createFromData($excelData){
        $data = [
            'addtime'     => date('Y-m-d H:i:s')
        ];
        $data = array_merge($data, $excelData);

        $result = self::insert($data);
        if ($result) {
            return self::id();
        }
        return false;
    }

    public static function getList($where)
    {
        $list = self::select('*', $where);

        if (empty($list)) {
            return [];
        }
        foreach ($list as &$info) {
            self::formatInfo($info);
        }

        return $list;
    }

    private static function formatInfo(&$info)
    {
        if (empty($info))
            return;
        foreach ($info as $key => $value) {
            switch ($key) {
                case 'result':
                    $info['result_desc'] = self::$resultDesc[$value];
                    break;
            }
        }
    }
}